#***********************
# Author: Yanfang Feng
# Compute the ratio
# Input format: excel
#***********************

import xlrd,xlwt
import numpy as np

import sys


def read_column_single(sheet,ncols,maxmin):
        data = []
        nrows = min(sheet.nrows,maxmin)
        for n in range(1,nrows):
                v1 = sheet.cell_value(n,ncols)
                if isinstance(v1,float):
                        data.append(v1)
        return data

# read two columns start at ncols
def read_column_double(sheet,ncols,maxmin):
        data = []
        nrows = min(sheet.nrows,maxmin)
        for n in range(1,nrows):
                v1 = sheet.cell_value(n,ncols)
                v2 = sheet.cell_value(n,ncols+1)
                if isinstance(v1,float):
                        v = (float(v1)+float(v2))/2.0
                        data.append(v)
        return data

# get the area under the curve_common
def common_area(sheet,ncols=[2,4],zero=True,minx=0,maxmin=62):

        if not isinstance(ncols,list):
                raise ValueError('ncols is not list')


        single = True
        if single == True:
                if ncols[0]%2==0: nc = ncols[0]
                else: nc = ncols[0]-1
                #print(ncols[0],'-->',nc)
                data1 = read_column_double(sheet,nc,maxmin)
                if ncols[1]%2==0: nc = ncols[1]
                else: nc = ncols[1]-1
                #print(ncols[1],'-->',nc)
                data2 = read_column_double(sheet,nc,maxmin)
        else:
                data1 = read_column_single(sheet,ncols[0],maxmin)
                data2 = read_column_single(sheet,ncols[1],maxmin)

        #print('anan:',len(data1),len(data2))

        minx1 = np.min(data1)
        minx2 = np.min(data2)

        minx = min(minx1,minx2)

        data1 = data1 - minx
        data2 = data2 - minx

        if np.any(data1<0):
                print(data1,minx)
        if np.any(data2<0):
                print(data2,minx)

        s1 = np.sum(data1)
        s2 = np.sum(data2)

        r = s1/s2
        return r


def get_minx(sheet,ncols=[2,4,6],zero=True,maxmin=62):

        if not isinstance(ncols,list):
                raise ValueError('ncols is not list')

        datalist = []
        minlist = []
        for nc in ncols:
                d = read_column_single(sheet,nc,maxmin)
                datalist.append(d)
                minlist.append(np.min(d))
                d = read_column_single(sheet,nc+1,maxmin)
                datalist.append(d)
                minlist.append(np.min(d))

        minv = np.min(minlist)
        return minv

def sheet_processing(inSheet,topn=45,zero=True,stype='ratio'):
        numtCols = inSheet.ncols

        numCols = numtCols
        for n in range(numtCols):
                v = inSheet.cell_value(0,n)
                if len(v) == 0:
                        numCols = n
                        break
                #print('cos:',n,v,len(v))

        print('get numCOls:',numCols)
        numCols = 6
        maxmin = 62

        if numCols >= 6:
                comx = get_minx(inSheet,ncols=[2,4],zero=zero,maxmin=maxmin)
                r1 = common_area(inSheet,ncols=[2,4],zero=zero,minx=comx,maxmin=maxmin)
                r2 = common_area(inSheet,ncols=[2,5],zero=zero,minx=comx,maxmin=maxmin)
                r3 = common_area(inSheet,ncols=[3,4],zero=zero,minx=comx,maxmin=maxmin)
                r4 = common_area(inSheet,ncols=[3,5],zero=zero,minx=comx,maxmin=maxmin)

                res=[[r1,r2,r3,r4]]

        if numCols >= 8:

                comx = get_minx(inSheet,ncols=[2,4,6],zero=zero,maxmin=maxmin)
                r11 = common_area(inSheet,ncols=[2,4],zero=zero,minx=comx,maxmin=maxmin)
                r12 = common_area(inSheet,ncols=[3,4],zero=zero,minx=comx,maxmin=maxmin)
                r13 = common_area(inSheet,ncols=[2,5],zero=zero,minx=comx,maxmin=maxmin)
                r14 = common_area(inSheet,ncols=[3,5],zero=zero,minx=comx,maxmin=maxmin)
                r21 = common_area(inSheet,ncols=[2,6],zero=zero,minx=comx,maxmin=maxmin)
                r22 = common_area(inSheet,ncols=[3,6],zero=zero,minx=comx,maxmin=maxmin)
                r23 = common_area(inSheet,ncols=[2,7],zero=zero,minx=comx,maxmin=maxmin)
                r24 = common_area(inSheet,ncols=[3,7],zero=zero,minx=comx,maxmin=maxmin)
                res=[[r11,r12,r13,r14],[r21,r22,r23,r24]]

        if numCols > 10:
                print('more colums')

        return res 
def excel_processing(fname,outfname):
        rbook = xlrd.open_workbook(fname)
        nsheet = len(rbook.sheet_names())

        wbook = xlwt.Workbook()

        stype = 'area_min'
        osheet = wbook.add_sheet('area_min')
        for n in range(nsheet):
                sheet = rbook.sheet_by_index(n)
                name = rbook.sheet_names()[n]

                print(stype,' name:',name)
                reslist = sheet_processing(sheet,topn=45,zero=False,stype=stype)
                osheet.write(n,0,name)
                index = 1

                for res in reslist:
                        for r in res:
                                osheet.write(n,index,r)
                                index += 1

                        minx = np.min(res)
                        maxx = np.max(res)
                        mean = np.mean(res)

                        index += 2
                        osheet.write(n,index,'min')
                        index += 1
                        osheet.write(n,index,minx)
                        index += 1
                        osheet.write(n,index,'max')
                        index += 1
                        osheet.write(n,index,maxx)
                        index += 1
                        osheet.write(n,index,'mean')
                        index += 1
                        osheet.write(n,index,mean)
                
                        index += 3

        wbook.save(outfname)

if __name__ == '__main__':
        if len(sys.argv) < 3:
                print('input parameters: input.xls output.xls')
                raise ValueError('%s input.xls out.xls'%(sys.argv[0]))

        fname = sys.argv[1]
        outfname = sys.argv[2]

        excel_processing(fname,outfname)

        



                




